<?php
define('DEFAULT_JS_DIR','js/');
define('DEFAULT_CSS_DIR','css/');
define('DEFAULT_IMG_DIR','js/');



function MakePhpCtl($type,$settings,$value = '',$options = ''){
	if(!empty($type)){
		$ctlClass = new $type($settings,$value,$options);
		$html = $ctlClass->render();
		return $html;
	}
}



function MakePhpCtlWithLabel($label,$type,$settings,$value = '',$options = ''){
	$html = '<label for="' . $settings['name'] . '">' . $label . "</label>\n";
	$html .= MakePhpCtl($type,$settings,$value,$options);
	return $html;

}



class CtlBase{
	public $settings;
	public $html;
	public $value;
	public $options;


	function ProcessSettingsArray(){
		if(is_array($this->settings)){
			foreach($this->settings as $setting=>$value){ // loop over att compute instead maybe
			
				if($this->ar_att_compute[$setting] != null){
					$setfunction = $this->ar_att_compute[$setting];
					unset ($this->settings[$setting]);			
					$this->$setfunction($value,$type);
				}
			}
		}		
	}

	function AttributesFromArray($settings,$type){

		if(is_array($settings)){
			foreach($settings as $setting=>$value){
				$html .= $setting . '="' . $value . '" ';
			}
		}
		else{
			$html .= $settings;
		}
		return $html;
	}



	function __construct($settings,$value='',$options=''){
		$this->settings = $settings;
		$this->value = $value;
		$this->options = $options;
		$this->ProcessSettingsArray();
		
		

	}
	
	function render(){
		return 'HTML here';
	}
	
}

// Not complete, but works for now for static text
class CtlSpan extends CtlBase {
	
	protected $ar_att_compute = array('do_after_update'=>'nothing');

	function nothing($value,$type){
	}
	
	function render(){
		$html .= "<span ";
		$html .= self::AttributesFromArray($this->settings,$type);
		$html .= ">";
		$html .= $this->value;
		$html .= "</span>";
		return $html;
	}

}

class CtlText extends CtlBase {
	
	
	protected $ar_att_compute = array('set_name'=>'set_name',
		 'set_on_change'=>'set_on_change',
		 'do_after_update'=>'do_after_update');	

	function do_after_update($value,$type){
		$this->settings['onblur'] = $value;
		//unset($this->settings['do_after_update']);
	}
		 
	function set_on_change($value,$type){
		$this->settings['onblur'] = $value;

	}

	
	function render(){
		$type = 'text';	
		$this->settings['value'] = $this->value;
	

		$html = "<input type=\"text\" ";
		$html .= self::AttributesFromArray($this->settings,$type);
		
		$html .= " />\n";
		return $html;
	}	
	
}


class CtlSelect extends CtlBase {

	protected $ar_att_compute = array('set_name'=>'set_name',
		 'set_on_change'=>'set_on_change',
		 'do_after_update'=>'do_after_update');	
	
	function do_after_update($value,$type){
		$this->settings['onchange'] = $value;
		unset($this->settings['do_after_update']);
	}
	
	function set_on_change($value,$type){
		$this->settings['onchange'] = $value;

	}
	
	function render(){
		$type = 'select';	

		$html = "<select ";
		$html .= self::AttributesFromArray($this->settings,$type);
		

		
		$html .= " />\n";
		$html .= self::makeOptions();
		$html .= "</select>\n";
		return $html;
	}	


	function makeOptions(){
		if(is_array($this->options)){
		foreach($this->options AS $optkey=>$option){
			if(is_array($option)){

				$html .= "<OPTION ";
				if ($this->value === $option['keyvalue']) {
					$html .= "selected=\"selected\" " ;
					}
				$html .= "value=\"";
				$html .= $option['keyvalue'];
				$html .="\">";
				//$html .= substr($item[$showvalue],0,$textlimit);
				$html .= $option['showvalue'];
				$html .="</OPTION>\n";

			}
			else{
				$html .= "<OPTION ";
				if ($this->value == $optkey) {
					$html .= "selected=\"selected\" " ;
					}
				$html .= "value=\"";
				$html .= $optkey;
				$html .="\">";
				$html .= $option;
				$html .="</OPTION>\n";	
			}
		} // end foreach $this->options	
		} // if is array
		return $html;
	}
	
}

class CtlJQDate extends CtlBase {
	protected $ar_att_compute = array('images_dir'=>'images_dir',
		 'set_on_change'=>'set_on_change',
		 'source_format'=>'source_format',
		 'show_format'=>'show_format',
		 'do_after_update'=>'set_on_change');	

	protected $images_dir = DEFAULT_IMG_DIR;
	protected $show_format = 'yy-mm-dd';
	protected $source_format = 'yy-mm-dd';
	protected $change_function = '';
	protected $id;
		 
	function render(){
		$this->set_id();
		$type = 'text';	
		$this->settings['value'] = $this->value;
	

		$html = "<input type=\"text\" ";
		$html .= self::AttributesFromArray($this->settings,$type);
		$html .= " />\n";
		
		$html .= $this->renderJS();		
		
		return $html;
	}	

	function images_dir($value,$type){
		$this->images_dir = $value;
	}

	function set_on_change($value,$type){
		$this->changefunction = $value;

	}
	
	 function set_id(){
		if(empty($this->settings['id'])){
			$this->settings['id'] = 'dateid1';
		}
	}
	// consider adding id function to provide default id
	
	function renderJS(){
		$eleid = $this->settings['id'];
	    $js .= '
<script type="text/javascript">
	$(function() {
		$("#' . $eleid . '").datepicker({
		dateFormat: "yy-mm-dd"';
	if($this->changefunction != ''){
	$js .= ',
		onSelect: function(dateText, inst){' . $this->changefunction . ' }'  ;
	}
	$js .= '
		});
	});
</script>
';
	return $js;
	}
	
}

class CtlTextArea extends CtlBase {
	
	
	protected $ar_att_compute = array('set_name'=>'set_name',
		 'set_on_change'=>'set_on_change',
		 'do_after_update'=>'set_on_change');	
	
	function set_on_change($value,$type){
		$this->settings['onblur'] = $value;

	}

	
	function render(){
		$type = 'text';	
		//$this->settings['value'] = $this->value;
	

		$html = "<textarea ";
		$html .= self::AttributesFromArray($this->settings,$type);
		
		$html .= " >\n";
		$html .= $this->value;
		$html .= "</textarea>";
		return $html;
	}	
	
}

class CtlCheckbox extends CtlBase {
	
	protected $ar_att_compute = array('set_name'=>'set_name',
		 'set_on_change'=>'set_on_change',
		 'set_checkvalue'=>'set_checkvalue',
		 'do_after_update'=>'set_on_change');	
	
	protected $checkvalue = 1;
	
	function set_checkvalue($value,$type){
		$this->checkvalue = $value;
	}
	
	
	
	function set_on_change($value,$type){
		$this->settings['onclick'] = $value;

	}

	
	function render(){
		$type = 'checkbox';	
		//$this->settings['value'] = $this->value;
	
		$html = "<input type='hidden' name='{$this->settings['name']}' value='{$this->value}' >\n";
		$html .= "<input type='checkbox' ";
		$html .= self::AttributesFromArray($this->settings,$type);
		if($this->value != 0){
			$html .= " checked = 'checked' ";
		}
		$html .= "value ='$this->value' ";
		$html .= " \>\n";

		return $html;
	}	


}

?>
